﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using FluentNHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;

namespace JCSoft.Collect.Data
{
    internal sealed class SessionFactory
    {
        private static ISessionFactory m_instance;
        private static object m_lock = new object();

        public static ISessionFactory CurrentFactory
        {
            get
            {
                if (m_instance == null)
                {
                    lock (m_lock)
                    {
                        if (m_instance == null)
                        {
                            m_instance = CreateSessionFactory();
                        }
                    }
                }

                return m_instance;
            }
        }

        private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2005
                    .ConnectionString(c => c.FromConnectionStringWithKey("CollectData"))
                    .ShowSql())
                .Mappings(m => m.FluentMappings.AddFromAssembly(typeof(SessionFactory).Assembly))
                .BuildSessionFactory();
        }
    }
}
